﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AspZone.Commerce.Model;
using AspZone.Commerce.Repositories;
using System.Transactions;

namespace AspZone.Commerce.IntegrationTests
{
    /// <summary>
    /// Summary description for CategoryRepositoryTest
    /// </summary>
    [TestClass]
    public class CategoryRepositoryTest
    {
        private TransactionScope transactionScope;
        private ICategoryRepository repository;
        private TestData.TestDataLoader dataLoader; 

        [TestInitialize]
        public void TestInitialize()
        {
            this.transactionScope = new TransactionScope(TransactionScopeOption.Required);
            this.repository = new CategoryRepository();
            this.dataLoader = new TestData.TestDataLoader();
            this.dataLoader.DeleteAllData();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            this.transactionScope.Dispose();
        }

        [TestMethod]
        public void Categories_Returns_Categories()
        {
            var c1 = this.dataLoader.InsertTestCategory("c1");
            var c2 = this.dataLoader.InsertTestCategory("c2");
            var c3 = this.dataLoader.InsertTestCategory("c3");

            var categories = this.repository.Categories;

            Assert.IsNotNull(categories);
            Assert.AreEqual<int>(3, categories.Count());
        }

        [TestMethod]
        public void Save_When_Passed_New_Category_Saves_Category()
        {
            var category = new Category { Name = "c1", SEOName = "c1" };
            
            this.repository.Save(category);

            var c2 = this.dataLoader.GetCategory(category.Id);
            Assert.IsNotNull(c2);
        }

        [TestMethod]
        public void Save_When_Passed_Existing_Category_Saves_Category()
        {
            this.dataLoader.InsertTestCategory("c1");

            var c1 = this.repository.Categories.First();
            c1.LongDescription = "Updated";
            this.repository.Save(c1);

            Assert.AreEqual<int>(1, this.repository.Categories.Count());
            Assert.AreEqual<int>(1, this.repository.Categories.Count(c => c.LongDescription == "Updated"));
        }
    }
}
